/**
 * 
 */
/***************************************************************
                           病人管理模块
***************************************************************/
var addPatDialog;
function patManagerInit()
{
	$("#patTable tr:nth-child(even)").addClass("trevencolor");
	BUI.use(['bui/overlay','bui/form'],function(Overlay,Form){
     	var form = new Form.HForm({
	       	srcNode : '#form'
   	}).render();
	 
	addPatDialog = new Overlay.Dialog({
        title:'添加患者',
        width:540,
        height:400,
        //配置DOM容器的编号
        contentId:'pat_content',
        cancel:function(){
	   	    choose_close();
	      	clearPatDialog();
	    },
        success:function () {
        	 saveOrUpdatePat();
           }
       });
   });
	 
   $("#add_Pat").bind("click",function(){
	   clearPatDialog();
	   addPatDialog.set("title","添加患者");
	   addPatDialog.show();
	   choose_open();
   });
   
   patValidate();
}

/**
 *	添加患者 type="系统自动生成"
 *  编辑患者 type=2
 */
function saveOrUpdatePat()
{
	var type = $("#pat_mrn").val();
	if($("#patForm").validate().form())
	{
		addPatDialog.close();
		var url = projectName + "/pat/saveOrUpdate.do?postDate=" + new Date().getTime();
		$.ajax({
			url: url,
			type: "post",
			async: false,
			data: $('#patForm').serialize(),
			success: function(data,result)
			{
				if(type == "系统自动生成")
				{
					if("ERROR" == data)
					{
						messageAlert(1,"添加患者失败，请重试",function()
								{
							this.close();
							choose_close();
							showPatPageDataList(1);
						});
						
					}
					else
					{
						messageAlert(1,"添加患者成功，病历号为："+data,function()
								{
									this.close();
									choose_close();
									showPatPageDataList(1);
//									var PatId = getPatId();
//									saveLog(PatId,1,1,2,PatName);
						});
					}
				}
				else
				{
					if("ERROR" == data){
						messageAlert(1,"修改患者失败，请重试",function()
								{
									this.close();
									choose_close();
									showPatPageDataList(1);
						});
					}
					else
					{
						messageAlert(1,"修改患者成功",function()
								{
									this.close();
									choose_close();
									showPatPageDataList(1);
//									var PatId = getPatId();
//									saveLog(PatId,1,1,3,PatName);
						});
					}
					
				}
				
			}
		});
		
	}
}

/**
 *	清空表单数据 
 */
function clearPatDialog(){
	patFormValidate.resetForm();
	$("#pat_mrn").val("系统自动生成");
	$("#pat_name").val("");
	$("#pat_id_card").val("");
	$("#pat_sex").val("男");
	$("#pat_age").val("");
	$("#pat_addr").val("");
	$("#pat_phone").val("");
}


/**
 *	删除患者 
 */
function deletePat(id)
{
//	var mgrEntity = getPatNames(id);
	$.ajax({
		 url: projectName+"/pat/deleteByIds.do?date=" + new Date().getTime(),
		 type: "post",
		 data: {"ids": id},
		 async: false,
		 success: function(data,result)
		 {
			 if("SUCCESS" == data){
				 messageAlert(1,"删除患者成功",function()
				 {
						this.close();
						choose_close();
						showPatPageDataList(1);
//						var PatId = getPatId();
//						saveLog(PatId,1,1,4,mgrEntity);
				 });
			 }
			 else if("ERROR" == data)
			 {
				 messageAlert(3,"删除患者失败",function(){
						this.close();
						choose_close();
						showPatPageDataList(1);
				 });
			 }
		 }
	 });
}

/**
 *	判断批量删除患者是否为空
 */
function del_pat(cbxName)
{
	var ids = getSelectCbx(cbxName);
	if(ids == "")
	{
		messageAlert(2,"请选择要删除的患者！",function(){choose_close(),this.close()});
	}
	else
	{
		messageAlert(4,'你确定要删除选择的患者吗？',function(){choose_open,deleteByIds(ids)},1)
	}
}

/**
 *	批量删除患者 
 */
function deleteByIds(ids){
//	var mgrEntity = getPatNames(ids);
	$.ajax({
		 url : projectName + "/pat/deleteByIds.do?date=" + new Date().getTime(),
		 type: "post",
		 data: {"ids": ids},
		 async: false,
		 success: function(data,result){
			 if("SUCCESS" == data){
				 messageAlert(1,"删除患者成功",function(){
						this.close();
						choose_close();
						showPatPageDataList(1);
//						var PatId = getPatId();
//						saveLog(PatId,1,1,4,mgrEntity);
				 });
			 }
			 
			 else if("ERROR" == data)
			 {
				 messageAlert(3,"删除患者失败",function(){
						this.close();
						choose_close();
						showPatPageDataList(1);
				 });
			 }
		 }
	 });
}

/**
 *	编辑患者 
 */
function patEdit(mrn)
{
	$.ajax({
		url : projectName+"/pat/getPatByMrn.do?date=" + new Date().getTime(),
		async : false,
		data:{"mrn":mrn},
		success : function(data) {
			if(null != data&&data != "")
			{
				clearPatDialog();
				$("#pat_mrn").val(data.mrn);
				$("#pat_name").val(data.name);
				$("#pat_id_card").val(data.id_card);
				$("#pat_sex").val(data.sex);
				$("#pat_age").val(data.age);
				$("#pat_addr").val(data.addr);
				$("#pat_phone").val(data.phone);
				choose_open();
				addPatDialog.set("title","编辑患者");
				addPatDialog.show();		
			}
			else
			{
				messageAlert(0,"该患者已被删除！",function(){
					choose_close(),
					this.close(),
					showPatPageDataList(1)
					});
			}
		}
	});
	
}

/**
 *	 导出
 */
function exportPat()
{
	var url = projectName+"/pat/exportPat.do?date=" + new Date().getTime();
	window.location.href = url;
}


/***************************************************************
							患者分页
***************************************************************/
/**
 *	患者分页对象 
 */
var patPageInfo={
		orderBy:null,
		order:"desc",
		pageNo:"1",
};


/**
 * 查询患者数据
 */
function showPatPageDataList(pageNo)
{
	patPageInfo.pageNo = pageNo;
	var search_mrn = getSearch_mrn("pat");
	var patPageInfoString = JSON.stringify(patPageInfo);
	window.location.href = projectName + "/pat/getPagePat.do?PageInfo=" + patPageInfoString + "&search_mrn=" + search_mrn;
}


/***************************************************************
							表单验证
***************************************************************/
var patFormValidate;
function patValidate()
{
	patFormValidate = $("#patForm").validate({
		errorPlacement: function(error, element) { 
         	error.appendTo(element.next());
         },
		rules:{
			name:
			{
				required:true,
				stringCheck:true,
				maxlength:20
			},
			id_card: 
			{
				required:true,
				isIdCardNo:true
			},
			age:
			{
				required:true,
			},
			addr:
			{
				required:true,
			},
			phone:
			{
				required:true,
			}
		},
		messages:{
			name:
			{
                required:"姓名不能为空",
                maxlength:"长度不要超过20个字符"
            },
            id_card:
            {
            	required:"身份证不能为空"
            },
            age:
			{
				required:"不能为空",
			},
			addr:
			{
				required:"不能为空",
			},
			phone:
			{
				required:"不能为空",
			}              
        }
		
	});
	
}